<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
*/
!defined('IN_MUDDER') && exit('Access Denied');

$_G['loader']->model('item:itembase', FALSE);
class msm_item_picture extends msm_item_itembase {

	var $table = 'dbpre_pictures';
	var $key = 'picid';

	function __construct() {
		parent::__construct();
		$this->model_flag = 'item';
		$this->init_field();
	}

	function init_field() {
		$this->add_field('albumid,sid,title,comments,url');
		$this->add_field_fun('albumid,sid', 'intval');
		$this->add_field_fun('title,comments,url', '_T');
	}

	function find($select, $where, $orderby, $start=0, $offset=0, $total = TRUE, $join_subject = FALSE) {
		if($where['pid']) $join_subject = TRUE;
		if($join_subject) {
			$this->db->join($this->table, 'p.sid', $this->subject_table, 's.sid');
		} else {
			$this->db->from($this->table,'p');
		}
		$this->db->where($where);

		$result = array(0,'');
		if($total) {
			if(!$result[0] = $this->db->count()) {
				return $result;
			}
			$this->db->sql_roll_back('from,where');
		}

		$this->db->select($select ? $select : '*');
		$this->db->order_by($orderby);
		if($offset>0) $this->db->limit($start, $offset);
		$result[1] = $this->db->get();
		return $result;
	}

	function save($post, $multi = FALSE, $setthumb = FALSE)
	{
		//权限判断
		if(!$this->in_admin) {
			if(!$this->global['user']->check_access('item_pictures', $this, false)) {
				return false;
			}
		}

		//关联主题的判断
		$subject = $this->check_post_before($post['sid'], FALSE, $setthumb);
		if($this->has_error()) return false;

		$this->subject = $subject;
		$pid = $subject['pid'];
		if($multi && !$post['title']) {
			$post['title'] = $subject['sid'] . '_'.rand(10000,99999);
		} elseif($this->in_admin && $this->in_ajax) {
			$post['title'] = $subject['sid'] . '_'.rand(10000,99999);
		}
		if(strtolower($post['url']) == 'http://') $post['url'] = '';

		$this->check_post($post);
		if($this->has_error()) return false;
		
		if($this->in_admin) {
			$post['uid'] = 0;
			$post['username'] = $this->global['admin']->adminname;
			$post['status'] = 1;
		} else {
			$post['uid'] = $this->global['user']->uid;
			$post['username'] = $this->global['user']->username;
			$category = $this->variable('category');
			$post['status'] = $category[$pid]['config']['picturecheck'] ? 0 : 1;
		}
		$post['addtime'] = $this->global['timestamp'];
		$post['city_id'] = $subject['city_id'];
		define('RETURN_EVENT_ID', $post['status'] ? 'global_op_succeed' : 'global_op_succeed_check');

		if(isset($post['download_src'])) { //远程下载
			$downsrc = $post['download_src'];
			unset($post['download_src']);
			$imginfo = $this->download($downsrc,false);
		}elseif(isset($post['local_picture'])) { //本机复制
			$local_picture = $post['local_picture'];
			unset($post['local_picture']);
			$imginfo = $this->local_picture($local_picture);
		} else { //常规上传
			$imginfo = $this->upload();
		}
		if($this->has_error()) return false;
		if(!$imginfo) return $this->add_error('item_post_picture_lost');

		$post = array_merge($post, $imginfo);
		$A =& $this->loader->model('item:album');
		if($post['albumid'] && !is_numeric($post['albumid'])) {
			$album_name = $post['albumid'];
			$post['albumid'] = $A->create_normal($post['sid'], $post['albumid'], $post['thumb']);
		}

		//未相册选择
		if(!$post['albumid']) {
			//存在相册
			if($alubm = $A->getlist($post['sid'],1)) {
				$post['albumid'] = $alubm['albumid'];
			} else {
				//不存在则新建
				$post['albumid'] = $A->create_normal($post['sid'],lang('item_album_name',$subject['name']),$post['thumb']);
			}
		}
		if($A->has_error()) return $this->add_error($A);

		$post   = $this->convert_post($post);
		$picid  = parent::save($post);

		if($post['status']) {
			$this->loader->model('item:album')->album_total($post['albumid'], 1, $post['thumb']);
			$this->subject_total_add($post['sid'], $post['thumb'], 1, $setthumb);
			$post['uid'] && $this->add_user_point($post['uid']);
			$post['uid'] && $this->_feed($picid); //添加feed事件
		}

		if(_post('do') == 'review_upload') {
			return $this->return_review_pic($picid, $post['thumb'], $post['filename']);
		}

		$A =& $this->loader->model('item:album');
		$A->lastupdate($post['albumid']);

		return $picid;
	}

	//删除图片
	function delete($picids, $delete_point = FALSE) {
		$ids = parent::get_keyids($picids);
		$where = array('picid' => $ids);
		if(!$delete_point && !$this->in_admin) $delete_point = TRUE;
		$this->_delete($where,$delete_point,true);
	}

	//删除主题所有图片
	function delete_subject($ids,$delete_point=false) {
		$ids = parent::get_keyids($ids);
		$where = array('sid'=>$ids);
		$this->_delete($where,$delete_point,false);
	}

	//删除相册图片
	function delete_album($ids,$update_total=true,$delete_point=false) {
		$ids = parent::get_keyids($ids);
		$where = array('albumid'=>$ids);
		$this->_delete($where,$delete_point,true);
	}

	//获取数量
	function count_album($albumid) {
		$this->db->from($this->table);
		$this->db->where('albumid', $albumid);
		$this->db->where('status',1);
		return $this->db->count();
	}

	function update($post) {
		if(empty($post)) redirect('global_op_unselect');
		$albumids = array();
		foreach($post as $key => $val) {
			if(!$key || !is_numeric($key) || $key < 1) continue;
			if(!$this->in_admin) {
				$p = $this->read($key);
				if($p['uid'] != $this->global['user']->uid &&
					!$this->loader->model('item:subject')->is_mysubject($p['sid'],$this->global['user']->uid)) {
					redirect('global_op_access');
				}
			}
			$post = $this->get_post($val);
			$this->check_post($post, TRUE);
			if($this->has_error()) redirect($this->error());

			$post = $this->convert_post($post);
			if(strtolower($post['url']) == 'http://') $post['url'] = '';
			$this->db->from($this->table);
			$this->db->set($post);
			$this->db->where('picid', $key);
			$this->db->update();
			//变更了相册
			if($val['albumid']>0 && !in_array($val['albumid'], $albumids)) {
				$albumids[] = $val['albumid'];
			}
		}
		if($albumids) {
			$A =& $this->loader->model('item:album');
			$A->rebuild($albumids);
		}
	}

	function upload()
	{
		//水印文字
		$author = $this->subject ? $this->subject['name'] : $this->global['user']->username;

		$upload_obj = ms_upload::image('picture', array(
			'watermark_text' => lang('item_picture_wtext', array(S('watermark_text'), $author)), //水印文字
		));
		//设置缩略图尺寸
		$config = $this->variable('config');
		$thumb_w = $config['pic_width'] ?: 200;
		$thumb_h = $config['pic_height'] ?: 150;
		$upload_obj->add_thumb('thumb', 'thumb_', $thumb_w, $thumb_h);
		if(!$upload_obj->upload('pictures')) {
			redirect($upload_obj->error());
		}

		$post = array();
		$post['filename']	= $upload_obj->url;
		$post['thumb']		= $upload_obj->thumb_filenames['thumb']['url'];
		$post['size']		= $upload_obj->filesize;
		$post['width']		= $upload_obj->width;
		$post['height']		= $upload_obj->height;

		return $post;
	}

	function download($src, $show_error=true) {
		$this->loader->lib('download_file', NULL, FALSE);
		$DL = new ms_download_file($src, $this->global['cfg']['picture_ext']);
		$dir_mod = $this->global['cfg']['picture_dir_mod'];
		$DL->download('pictures',$dir_mod);

		$filename = MUDDER_ROOT.$DL->path.'/'.$DL->filename;
		if(function_exists('getimagesize') && !@getimagesize($filename)) {
			$DL->delete_file();
			redirect('global_upload_image_unkown');
		}
		
		$post = array();
		list($post['width'],$post['height'],,) = @getimagesize($filename);
		$post['filename'] = $post['thumb'] = str_replace(DS, '/', $DL->path . '/' . $DL->filename);
		$post['size'] = @filesize($filename);

		return $post;
	}

	//从本机（服务器）其他文件夹复制
	function local_picture($localpic) {
		if(!$localpic) return null;
		$result = array();

		$filename = $this->move_local_picture($localpic);
		if(!$filename) return null;
		$filepath = MUDDER_ROOT . $filename;

		$post = array();
		list($post['width'],$post['height'],,) = @getimagesize($filepath);
		$post['filename'] = str_replace(DS, '/', $filename);
		$thumb = dirname($filename) . DS . 'thumb_' . basename($filename);
		$post['thumb'] = str_replace(DS, '/', $thumb);
		$post['size'] = @filesize($filepath);
		return $post;
	}

	function move_local_picture($pic) {
		$sorcuefile = MUDDER_ROOT . $pic;
		if(!is_file($sorcuefile)) {
			return false;
		}
		if(function_exists('getimagesize') && !@getimagesize($sorcuefile)) {
			@unlink($sorcuefile);
			return false;
		}

		$IMG = new ms_image();
		$IMG->watermark_postion = S('watermark_postion');
		$IMG->thumb_mod = S('picture_createthumb_mod');
		$IMG->set_thumb_level(S('picture_createthumb_level'));
		$wtext = S('watermark_text') ? S('watermark_text') : S('sitename');
		if($this->global['user']->username) {
			$IMG->set_watermark_text(lang('item_picture_wtext',array($wtext, $this->global['user']->username)));
		} else {
			$IMG->set_watermark_text(S('sitename'));
		}

		$name = basename($sorcuefile);
		$path = 'uploads';

		$picture_dir_mod = S('picture_dir_mod');
		if($picture_dir_mod == 'WEEK') {
			$subdir = date('Y', _G('timestamp')) . '-week-' . date('W', _G('timestamp'));
		} elseif($picture_dir_mod == 'DAY') {
			$subdir = date('Y-m-d', _G('timestamp'));
		} else {
			$subdir = date('Y-m', _G('timestamp'));
		}

		$subdir = 'pictures' . DS . $subdir;
		$dirs = explode(DS, $subdir);
		foreach ($dirs as $val) {
			$path .= DS . $val;
			if(!@is_dir(MUDDER_ROOT . $path)) {
				if(!mkdir(MUDDER_ROOT . $path, 0777)) {
					show_error(lang('global_mkdir_no_access',$path));
				}
			}
		}
		$result = array();
		$filename = $path . DS . $name;
		$picture = str_replace(DS, '/', $filename);
		if(!copy($sorcuefile, MUDDER_ROOT . $filename)) {
			return false;
		}
		if(S('watermark')) {
			$wfile = MUDDER_ROOT . 'static' . DS . 'images' . DS . 'watermark.png';
			$IMG->watermark(MUDDER_ROOT.$filename, MUDDER_ROOT.$filename, $wfile);
		}

		$thumb_w = $this->modcfg['thumb_width'] ? $this->modcfg['thumb_width'] : 200;
		$thumb_h = $this->modcfg['thumb_height'] ? $this->modcfg['thumb_height'] : 150;
		$dest_img_file = $path . DS . 'thumb_' . $name;
		$IMG->thumb($sorcuefile, MUDDER_ROOT . $dest_img_file, $thumb_w, $thumb_h);

		if(!DEBUG) @unlink($sorcuefile);
		return $picture;
	}
	
	function checkup($picids) {
		if(empty($picids)) redirect('global_op_unselect');
		if(!is_array($picids)) $picids = array((int)$picids);
		$picids = parent::get_keyids($picids);
		$this->db->select('picid,albumid,sid,status,uid,thumb');
		$this->db->from($this->table);
		$this->db->where_in('picid', $picids);
		$this->db->where('status', 0);
		if(!$row = $this->db->get()) return;
		$uids = $upids = $albums = array();
		$thumb = '';
		while ($value = $row->fetch_array()) {
			$upids[] = $value['picid'];
			//更新主题记录
			$this->subject_total_add($value['sid'], $value['thumb']);
			//累计相册添加数量
			if(isset($albums[$value['albumid']])) {
				$albums[$value['albumid']]['total']++;
			} else {
				$albums[$value['albumid']]['total'] = 1;
				$albums[$value['albumid']]['thumb'] = $value['thumb'];
			}
			//记录需要增加积分的用户和数量
			if($value['uid']) {
				if(isset($uids[$value['uid']])) {
					$uids[$value['uid']]++;
				} else {
					$uids[$value['uid']] = 1;
				}
				$this->_feed($value['picid']); //feed事件
			}
		}
		$row->free_result();
		//更新记录
		if($upids) {
			$this->db->from($this->table);
			$this->db->set('status', 1);
			$this->db->where_in('picid', $upids);
			$this->db->update();
		}
		//更新相册数据
		if($albums) {
			$A =& $this->loader->model('item:album');
			foreach ($albums as $albumid => $value) {
				$A->album_total($albumid, $value['total'], $value['thumb']);
			}
		}
		//增加用户积分
		if($uids) {
			foreach($uids as $uid => $num) {
				$this->add_user_point($uid, $num);
			}
		}
	}

	function check_post_before($sid, $isedit = FALSE, $setthumb = FALSE) {
		if(!$sid || !is_numeric($sid)) {
			return $this->add_error(lang('global_sql_keyid_invalid', 'sid'));
		}
		$this->db->from($this->subject_table);
		$this->db->select('sid,city_id,pid,catid,name,subname,status');
		$this->db->where('sid', $sid);
		if(!$detail = $this->db->get_one()) return $this->add_error('item_empty');
		if(!$setthumb && $detail['status'] != '1') return $this->add_error('item_picture_status_invalid');

		return $detail;
	}

	function check_post(& $post, $isedit = FALSE) {
		//sid,modelid,title,comments
		if(!$post['title']) return $this->add_error('item_picture_empty_title');
		//if(strlen($post['title']) > 255) redirect(lang('item_picture_title_charlen', 255));
	}

	//返回点评上传图片需要的格式
	function return_review_pic($picid, $thumb, $picture) {
		return "{ picid:\"$picid\",thumb:\"$thumb\",picture:\"$picture\"}";
	}

	// 增加主题的图片数量统计，并尝试设置主题封面
	function subject_total_add($sid, $thumb='', $num=1, $isthumb=FALSE) {
		// 新上传的图片是否这是为封面
		$set_thumb = FALSE;
		if($thumb) {
			if($isthumb) {
				$set_thumb = TRUE;
			} else {
				$modcfg = $this->variable('config');
				if($modcfg['thumb'] == '1') $set_thumb = TRUE;
				if($modcfg['thumb'] == '2') {
					$this->db->from($this->subject_table);
					$this->db->select('thumb,pictures');
					$this->db->where('sid', $sid);
					if(!$subject = $this->db->get_one()) return;
					// 没有设置或者图片不存在
					if(!$subject['thumb'] || !is_file(MUDDER_ROOT . $subject['thumb'])) {
						$set_thumb = TRUE;
					}
				}
			}
		}
		//更新主题
		$this->db->from($this->subject_table);
		$this->db->where('sid', $sid);
		$this->db->set_add('pictures');
		$set_thumb && $this->db->set('thumb', $thumb);
		$this->db->update();
	}

	function subject_total_dec($sid, $num=1) {
		$this->db->from($this->subject_table);
		$this->db->set_dec('pictures');
		$this->db->where('sid', $sid);
		$this->db->update();
	}

	function add_user_point($uid, $num = 1) {
		if(!$uid) return;
		$this->loader->model('member:point')
			->update_point($uid, 'add_picture', FALSE, $num, FALSE, array('pictures'=>array('set_add',array($num))));
	}

	function dec_user_point($uid, $num = 1) {
		if(!$uid) return;
		$this->loader->model('member:point')
			->update_point($uid, 'add_picture', TRUE, $num, FALSE, array('pictures'=>array('set_dec',array($num))));
	}

	//检测图片上传权限
	function check_access($key, $value, $jump) {
		if($this->in_admin) return TRUE;
		if($key=='item_pictures') {
			$value = (int) $value;
			if($value && $value < 0) {
				if(!$jump) return $this->add_error('item_access_alow_picture');
				redirect('item_access_alow_picture');
			}
			if($value && $value < $this->get_user_pictures()) {
				if(!$jump) return $this->add_error('item_access_pictures');
				redirect('item_access_pictures');
			}
		}
		return TRUE;
	}

	function get_user_pictures() {
		$this->db->where('uid', $this->global['user']->uid);
		$this->db->from($this->table);
		return $this->db->count();
	}

	//获取指定相册的第一张图片
	function get_album_first_pic($albumid,$orderby = array('picid'=>'DESC')) {
		$this->db->from($this->table);
		$this->db->where('albumid',$albumid);
		$this->db->where('status',1);
		$this->db->order_by($orderby);
		return $this->db->get_one();
	}

	//删除图片
	function _delete($where,$delete_point,$update_total) {

		$this->db->from($this->table);
		$this->db->where($where);
		if(!$q = $this->db->get()) return;

		//主题管理员
		if(!$this->in_admin) {
			$S =& $this->loader->model('item:subject');
			$mysubjects = $S->mysubject($this->global['user']->uid);
		}

		$A =& $this->loader->model('item:album');
		while($value=$q->fetch_array()) {
			//判断权限(后台管理员,留言会员以及主题管理员)
			$access = $this->in_admin || in_array($value['sid'], $mysubjects) || $this->global['user']->uid == $value['uid'];
			if(!$access) redirect('global_op_access');
			$deleteids[] = $value['picid'];
			if($value['status']) {
				//更新主题图片统计
				if($update_total) {
					$A->album_total($value['albumid'], 1);
					$this->subject_total_dec($value['sid']);
				}
				//记录需要删除积分的用户和数量
				if($value['uid'] && $delete_point) {
					if(isset($uids[$value['uid']])) {
						$uids[$value['uid']]++;
					} else {
						$uids[$value['uid']] = 1;
					}
				}
			}
			//删除图片，防止误删目录，做了字符数量判断
			if(strlen($value['filename']) > 12) {
				@unlink(MUDDER_ROOT . $value['filename']);
			}
			if(strlen($value['thumb']) > 12) {
				@unlink(MUDDER_ROOT . $value['thumb']);
			}
		}

		//删除记录
		if($deleteids) {
			$this->db->from($this->table);
			$this->db->where_in('picid', $deleteids);
			$this->db->delete();
		}

		//删除用户的对应积分
		if($delete_point && $uids) {
			foreach($uids as $uid => $num) {
				$this->dec_user_point($uid, $num);
			}
		}

	}

	//事件
	function _feed($picid) {
		//my_just_feeds
		if(!$picid) return;
		$flag = 'item:picture';

		$FEED =& $this->loader->model('member:feed');
		if(!$FEED->enabled()) return;
		$this->global['fullalways'] = TRUE;

		if(!$detail = $this->read($picid)) return;
		if(!$detail['uid']) return;

		$root = S('siteurl');
		if(substr($root,0,-1)!='/') $root .= '/';

		//获取5分钟内当前用户是否进行了相同的行为
		$num = 1;
		$maxnum = 5;
		$images = array();
		$images[] = array(
		   'url' => $root . $detail['thumb'],
		   'link' => url('item/album/picid/'.$picid),
		);
		$interval = 5 * 60;
		$jestfeed = $FEED->my_just_feed($detail['uid'], $flag);
		$del = false;
		if($interval && $jestfeed['dateline'] + $interval > $this->global['timestamp']) {
			$del = true;
			if($tmp = @unserialize($jestfeed['images'])) {
				$num = min(count($tmp) + 1, $maxnum);
				foreach($tmp as $v) if($maxnum-- > 1) $images[] = $v;
			}
		}

		$feed = array();
		$feed['icon'] = lang('item_picture_feed_icon');
		$feed['title_template'] = lang('item_picture_feed_title_template');
		$feed['title_data'] = array (
			'username' => '<a href="'.url("space/index/uid/$detail[uid]").'">' . $detail['username'] . '</a>',
			'num' => $num,
		);
		$feed['body_template'] = lang('item_picture_feed_body_template');
		$feed['body_general'] = '';
		$feed['images'] = $images;

		$FEED->save($flag, $detail['city_id'], $feed['icon'], $detail['uid'], $detail['username'], $feed);
		$del && $FEED->delete($jestfeed['id']);
	}

}
?>